package com.yinhe.music.yhmusic.login.phonelogin;

import android.content.Intent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.login.validcode.PhoneNumberActivity;
import com.yinhe.music.yhmusic.model.LoginInfo;

import butterknife.BindView;

/**
 * 登录界面
 *
 * @author 何晓华
 * @date 2017/9/25
 */

public class LoginActivity extends BaseServiceActivity implements LoginContract.ILoginView {

    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.next)
    Button next;
    @BindView(R.id.reset_pwd)
    TextView resetPwd;
    @BindView(R.id.reset_layout)
    LinearLayout resetLayout;

    LoginPresenter loginPresenter;

    @Override
    public void initView() {
        RxBus.get().register(this);

        password.setHint("密码");
        next.setText("登录");
        resetLayout.setVisibility(View.VISIBLE);
        phone.setFocusable(true);
        phone.requestFocus();
        phone.setInputType(EditorInfo.TYPE_CLASS_PHONE);

        setToolbar("手机号登陆");

        next.setOnClickListener(view -> login());
        resetLayout.setOnClickListener(view -> startReset());
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_register_login;
    }

    @Override
    public BasePresenter getPresenter() {
        return loginPresenter;
    }

    @Override
    public void initPresenter() {
        loginPresenter = new LoginPresenter();
        loginPresenter.attachModelView(new BaseModel(), this);
    }

    private void login() {
        String mUserName = phone.getText().toString().trim();
        String mPassword = password.getText().toString();
        loginPresenter.requestLogin(mUserName, mPassword);
    }

    private void startReset() {
        Intent intent = new Intent(this, PhoneNumberActivity.class);
        startActivity(intent);
    }

    @Override
    public void setLogin(LoginInfo loginInfo) {
        loginPresenter.saveLoginInfo(loginInfo);
        finish();
    }
}
